// pages/chat/chat.js
const app = getApp()
const api = require('../../utils/api')
Page({

	/**
	 * 页面的初始数据
	 */
	data: {
		userMsg: '',
		userCont: '',
		msgData: null,
		djId: null, //代驾id
		userId: null, //用户id
		djUserData: {},
		userData: {},
		allMsgData: [], //离开聊天页面获取的消息内容
		chatData: [], //聊天数据
		mrtx: 'https://cdn.oss.dgddaijia.com/icons/personal.png', //默认头像
		backBottom: '', //消息回底
		params: {},
		order: {},
		scrollTop: ""
	},
	sendMsg() {
		// 发送消息
		// im   id  msg  index
		var that = this;
		if (that.data.userCont == '') {
			wx.showToast({
				title: '请输入内容',
				icon: 'none'
			})
		} else {
			var im = 'im';
			var data = {
				user_id: that.data.userId,
				id: that.data.djId,
				msg: that.data.userCont,
				index: that.data.chatData.length
			}
			app.sendMessage(im, data)
		}
	},
	// 获取容器高度，使页面滚动到容器底部
	pageScrollToBottom: function() {
		var that = this;
		if (this.data.chatData[this.data.chatData.length - 1].id != undefined) {
			var id = this.data.chatData[this.data.chatData.length - 1]
				.id; // console.log('reesss', id, this.data.chatData.length)
			setTimeout(function() {
				that.setData({
					scrollTop: "msg-" + id
				})
			}, 500)
		}
	},
	getMsg(e) {
		// 获取用户输入的内容
		this.setData({
			userCont: e.detail.value
		})
	},
	/**
	 * 生命周期函数--监听页面加载
	 */
	getData() {
		var that = this;
		// https://dageda.api.hjdz.ltd/userdjorder/curorder
		var apis = api.api;
		var url = apis.host + 'userdjorder/curorder';
		var data = {};
		apis.request(url, data, 'POST').then(function(res) {
			if (res.code == 200) {
				apis.request(apis.host + 'sys/userinfo', {
					id: res.data.dj_id
				}, 'POST').then(function(res) {
					that.setData({
						djUserData: res.data,
					})
				}, function(eor) {})
				apis.request(apis.host + 'sys/userinfo', {
					id: res.data.user_id
				}, 'POST').then(function(res) {
					that.setData({
						userData: res.data,
					})
				})
				let chatData = wx.getStorageSync('chatlog_' + res.data.dj_id + '_id_' + res.data
					.user_id);
				if (!chatData) chatData = [];
				that.setData({
					djId: res.data.dj_id,
					userId: res.data.user_id,
					chatData: chatData,
					order: res.data,
					params: {
						order_id: res.data.id
					}
				})
				app.sendMessage('getLog', {
					id: res.data.user_id,
					touser: res.data.dj_id,
				})
			} else {

			}
		})
	},
	onLoad: function(options) {
		var that = this;
	},
	/**
	 * 生命周期函数--监听页面显示
	 */
	onShow: function() {
		var that = this;
		that.getData()
		app.messageEvent = function(event, data) {
			/**
			 * @var string event	事件名称
			 * @var object data	返回的数据
			 */
			switch (event) {
				case "im":
					app.sendMessage('imback', {
						id: that.data.dj_id,
						index: data.index,
						linkid: data.linkid,
						form_id: data.form_id
					})
					/*
					form_id: 4
					index: 0
					linkid: "7f00000107d0000085db"
					msg: "当饭吃"
					msgid: "dgdtu4u27c130"
					*/
					var data = {
						msg: data.msg,
						index: data.index,
						param: data.param,
						msgid: data.msgid,
						sendType: 'form',
						id: data.msgid
					};
					that.data.chatData.push(data)
					var count = that.data.chatData;
					that.data.chatData = that.data.chatData.splice((count - 1) - 50);
					// //接收代驾信息
					that.setData({
						msgData: data,
						chatData: that.data.chatData
					})
					that.pageScrollToBottom()
					return true;
					break;
				case "imback":
					// if (Number(data.send === 1)) {
					// 	if (data.index) that.data.chatData[data.index].status = 2
					// } else {
					// 	if (data.index) that.data.chatData[data.index].read = 1
					// }
					return true;
					break;
				case "imid":
					//接收当前消息返回的消息ID
					var data = {
						user_id: data.user_id,
						id: data.id,
						msg: data.msg,
						index: data.index,
						param: data.param,
						msgid: data.msgid,
						sendType: 'to',
					};
					that.data.chatData.push(data)
					var count = that.data.chatData;
					that.data.chatData = that.data.chatData.splice((count - 1) - 50);
					that.setData({
						userMsg: that.data.userCont,
						userCont: '',
						chatData: that.data.chatData
					})
					that.pageScrollToBottom()
					break;
				case "curimlog":
					//接收当前消息返回的消息ID
					// data.map((value, index) => {

					// 	console.log('value', value.id)
					// 	console.log('index', index)
					// })
					var chat = that.data.chatData;
					var allMsg = data;
					for (var i = 0; i < allMsg.length; i++) {
						let aa = false; //假定，不存在
						for (var j = 0; j < chat.length; j++) {
							if (chat[j].id == allMsg[i].id) {
								aa = true;
								break;
							}
						}
						if (!aa) {
							chat.push(allMsg[i])
						}
					}
					var count = chat.length;
					chat = chat.splice((count - 1) - 50);

					that.setData({
						allMsgData: data,
						chatData: chat
					})
					that.pageScrollToBottom()
					break;
			}
		}
	},

	/**
	 * 生命周期函数--监听页面隐藏
	 */
	onHide: function() {
		wx.setStorageSync('chatlog_' + this.data.djId + '_id_' + this.data.userId, this.data.chatData)
	},

	/**
	 * 生命周期函数--监听页面卸载
	 */
	onUnload: function() {
		wx.setStorageSync('chatlog_' + this.data.djId + '_id_' + this.data.userId, this.data.chatData)
	},

	/**
	 * 页面相关事件处理函数--监听用户下拉动作
	 */
	onPullDownRefresh: function() {

	},

	/**
	 * 页面上拉触底事件的处理函数
	 */
	onReachBottom: function() {

	},
})
